var firstTimeUsername = 0;
var lastTimeUsername = 0;
var keyStrokesUsername = 0;

var firstTimePassword = 0;
var lastTimePassword = 0;
var keyStrokesPassword = 0;

function do_submit()
{
	var n = document.getElementById("uname");
	var p = document.getElementById("upass");
	
	if (n.value == "" || p.value == "")
		alert("Please Enter Name and Password");
		
	bCodeName = navigator.appCodeName;
	bName = navigator.appName;
	bVer = navigator.appVersion;
	cookies = navigator.cookieEnabled;
	platform = navigator.platform;
	uAgent = navigator.userAgent;

	//txtOut = "Post:\n\nUser Name: " + n.value + "\nUser Password: " + p.value + "\nBrowser Code Name: " + bCodeName + "\nBrowser Name: " + bName + "\nBrowser Version: " + bVer +  "\nCookies Enabled?: " + cookies + "\nPlatform: " + platform + "\nDeltaTime: " + (lastTimePassword-firstTimePassword);
	
	xmlOut = "<login>";
	xmlOut = "<name>" + n.value + "</name>";
	xmlOut += "<password>" + p.value + "</password>";
	xmlOut += "<browser>" + BrowserDetect.browser + "</browser>";
	xmlOut += "<browserVersion>" + BrowserDetect.version + "</browserVersion>";
	xmlOut += "<platform>" + navigator.platform + "</platform>";
	xmlOut += "<passWordTime>" + (lastTimeUsername-firstTimeUsername) + "</passWordTime>";
	xmlOut += "<passWordKeyStrokes>" + keyStrokesPassword + "</passWordKeyStrokes>";
	xmlOut += "<nameWordTime>" + (lastTimePassword-firstTimePassword) + "</nameWordTime>";
	xmlOut += "<nameWordKeyStrokes>" + keyStrokesPassword + "</nameWordKeyStrokes>";
	xmlOut += "<userAgent>" + uAgent + "</userAgent>";
	xmlOut += "</login>";
	
	form = document.createElement('form');
    form.setAttribute('method', 'POST');
    form.setAttribute('action', 'post.php');
    myvar = document.createElement('input');
    myvar.setAttribute('name', 'data');
    myvar.setAttribute('value', xmlOut);
    myvar.setAttribute('type', 'hidden');
    form.appendChild(myvar);
    document.body.appendChild(form);
    form.submit();
}

function do_reset()
{
	//document.getElementById("Reset").innerHTML=Date();
}

function do_key_upass(e)
{
	var now = new Date()
	var keyID = e.keyCode;
	
	if (keyID == 13)
		do_submit();
	
	if (keyID == 8 || keyID == 46)
	{
		document.getElementById("upass").value = "";
		keyStrokesPassword = 0;
		firstTimePassword = now.getTime();
		lastTimePassword = now.getTime();
	}
	else if (firstTimePassword == 0)
	{
		firstTimePassword = now.getTime();
		lastTimePassword = now.getTime();
	}
	else
		lastTimePassword = now.getTime();	
	keyStrokesPassword += 1;

}

function do_key_uname(e)
{
	var now = new Date()
	var keyID = e.keyCode;
	
	if (keyID == 13)
		do_submit();
		
	if (keyID == 8 || keyID == 46)
	{
		document.getElementById("uname").value = "";
		keyStrokesUsername = 0;
		firstTimeUsername = now.getTime();
		lastTimeUsername = now.getTime();
	}
	else if (firstTimeUsername == 0)
	{
		firstTimeUsername = now.getTime();
		lastTimeUsername = now.getTime();
	}
	else
		lastTimeUsername = now.getTime();	
	keyStrokesUsername += 1;

}

//Below code taken from examples on http://www.quirksmode.org/js/detect.html accessed on May 1, 2011
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();